<template>
  <view>
    <!-- 选择收货地址区域 -->
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" @click="chooseAddress">请选择收货地址+</button>
    </view>

    <!-- 展示收货地址区域 -->
    <view class="address-info-box" v-else @click="chooseAddress">
      <!-- 第一行 -->
      <view class="row1">
        <view class="row1-left">
          <view class="user">收货人 : {{address.userName}}</view>
        </view>
        <view class="row1-right">
          <view class="phone">电话 : {{address.telNumber}}</view>
          <uni-icons type="arrowright"></uni-icons>
        </view>
      </view>
      <!-- 第二行 -->
      <view class="row2">
        <view class="row2-left">收货地址：</view>
        <view class="row2-right">{{addstr}}</view>
      </view>
    </view>

    <!-- 底部边框区域 -->
    <image src="../../static/cart_border@2x.png" class="border-bottom"></image>
  </view>
</template>

<script>
  import user from '../../store/user.js'
  export default {
    name: "my-address",
    computed: {
      address() {
        return user.state.address
      },
      addstr() {
        return user.getters.addstr
      },
    },
    data() {
      return {
        // address: {}
      };
    },
    methods: {
      // 选择收货地址
      chooseAddress() {
        uni.chooseAddress({
          success: (res) => {
            // this.address = res;
            // console.log(res);
            this.updateAddress(res)
          },
        })
      },
      // 引入user仓库中的 updateAddress 方法
      updateAddress(address) {
        user.commit('updateAddress', address)
      },
    }
  }
</script>

<style lang="scss">
  .address-choose-box {
    height: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .address-info-box {
    display: flex;
    flex-direction: column;
    font-size: 14px;
    margin-top: 10px;
    padding: 7px;

    .row1 {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;

      .row1-right {
        display: flex;
      }
    }

    .row2 {
      display: flex;

      .row2-left {
        white-space: nowrap;
      }
    }
  }

  .border-bottom {
    width: 100%;
    height: 5px;
  }
</style>